package fun.chutianshu.eescore_06_druid_mybatis_mybatisplus.controller;


import com.baomidou.mybatisplus.core.toolkit.Constants;
import fun.chutianshu.eescore_06_druid_mybatis_mybatisplus.dao.StudentDao;
import fun.chutianshu.eescore_06_druid_mybatis_mybatisplus.entity.Student;
import fun.chutianshu.eescore_06_druid_mybatis_mybatisplus.mappers.StudentMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="学生信息api")
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentDao studentDao;
    @Autowired
    StudentMapper studentMapper;

    @ApiOperation("返回所有学生")
    @GetMapping("/stu_list")
    @CrossOrigin
    public List<Student> index() {
        List<Student> studentList = studentMapper.selectList(null);
        return studentList;
    }

    @ApiOperation("根据学生姓名找学生")
    @PostMapping(path="/find_stu_by_name")
    @CrossOrigin
    public List<Student> find_stu_by_name(Student stu) {

        List<Student> studentList = (List<Student>) studentMapper.selectById(stu.getStu_code());
        return studentList;
    }

    //根据学生编号更新学生信息
    //返回受影响行数
    @ApiOperation("更新学生信息")
    @PostMapping(path="/update_stu_by_code")
    @CrossOrigin
    public int update_stu_by_code(Student stu) {
        return studentMapper.update(stu,null);
    }

    //添加一个新学生到数据库
    //返回受影响行数
    @ApiOperation("新增一个学生")
    @PostMapping(path="/add_one")
    @CrossOrigin
    public int add_one(Student stu) {
        return studentMapper.insert(stu);
    }

    //删除一个新学生
    //返回受影响行数
    @ApiOperation("删除一个学生")
    @PostMapping(path="/delete_one")
    @CrossOrigin
    public int delete_one(Student stu) {
        return studentMapper.deleteById(stu.getStu_code());
    }
}
